package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSON;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.bean.YearlyInspectionBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.LicencePlateMapper;
import com.molichuxing.services.infrastructure.dao.YearlyInspectMapper;
import com.molichuxing.services.infrastructure.dto.request.create.YearlyInspectionCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.YearlyInspectionModifyDto;
import com.molichuxing.services.infrastructure.dto.response.CarDto;
import com.molichuxing.services.infrastructure.dto.response.LicencePlateDto;
import com.molichuxing.services.infrastructure.dto.response.YearlyInspectionDto;
import com.molichuxing.services.infrastructure.service.LicencePlateService;
import com.molichuxing.services.infrastructure.service.YearlyInspectService;
import com.molichuxing.services.property.CarYearlyInspectStatusEnum;
import com.molichuxing.services.property.YearlyInspectHandleStatusEnum;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 车牌 服务实现类
 * </p>
 *
 * @author
 * @since 2019-08-15
 */
@Service("yearlyInspectService")
public class YearlyInspectServiceImpl implements YearlyInspectService {

  private static final Logger log = Logger.getLogger(YearlyInspectServiceImpl.class);

  @Resource
  private YearlyInspectMapper yearlyInspectMapper;

  @Override
  public Paged<YearlyInspectionDto> getYearlyInspectionPage(CarYearlyInspectStatusEnum carYearlyInspectStatusEnum, List<Integer> carIds,Integer stockId, String inspectStartDate, String inspectEndDate,Integer handleStatus, String sortName, String sortWay, Integer pageNum, Integer pageSize) {
    log.info("根据条件查询年检列表 carYearlyInspectStatusEnum ： ["+ carYearlyInspectStatusEnum +"]");
      Paged<YearlyInspectionDto> paged = new Paged<>();
      //paged.setTab(getTabList());
      if(null != stockId && stockId == -1) {
          return paged;
      }
    int count = yearlyInspectMapper.getYearlyInspectPageCount(carYearlyInspectStatusEnum.getValue(), carIds, inspectStartDate, inspectEndDate,handleStatus);
    if(count <= 0) {
        return paged;
    }
    log.info("根据条件查询年检列表 入参：queryWrapper： "+ JSON.toJSONString(carYearlyInspectStatusEnum));
    List<YearlyInspectionBean> result = yearlyInspectMapper.getYearlyInspectPage(carYearlyInspectStatusEnum.getValue(), carIds, inspectStartDate, inspectEndDate,handleStatus, sortName, sortWay, pageNum, pageSize);
    log.info("根据条件查询年检列表 出参：result： "+ JSON.toJSONString(result));
      paged.setList(Convert.toYearlyInspectionDto(result));
      paged.setTotal(count);
      paged.setPageSize(pageSize);
    return paged;
  }

  public static void main(String[] args) {
    Integer stockId = -1;
    System.out.println(stockId == -1);
  }

  @Override
  public YearlyInspectionDto getYearlyInspectionByCarId(Integer carId) {
    return Convert.toYearlyInspectDto(yearlyInspectMapper.getById(carId));
  }

  @Override
  public int createYearlyInspection(YearlyInspectionCreateDto createDto) {
    return yearlyInspectMapper.create(Convert.toYearlyInspectionBean(createDto));
  }

  @Override
  public int modifyYearlyInspection(YearlyInspectionModifyDto createDto) {
    return yearlyInspectMapper.modify(Convert.toYearlyInspectionBean(createDto));
  }

    @Override
    public int getYearlyInspectCount(CarYearlyInspectStatusEnum statusEnum) {
        return yearlyInspectMapper.getYearlyInspectCount(statusEnum.getValue());
    }

  @Override
  public List<YearlyInspectionDto> getYearlyInspectionList(List<String> inspectionIdList,CarYearlyInspectStatusEnum carYearlyInspectStatusEnum, List<Integer> carIds,Integer stockId, String inspectStartDate, String inspectEndDate) {
    if(null != stockId && stockId == -1) {
      return null;
    }
    List<YearlyInspectionBean> result = yearlyInspectMapper.getYearlyInspectList(inspectionIdList,carYearlyInspectStatusEnum.getValue(), carIds, inspectStartDate, inspectEndDate);
    return Convert.toYearlyInspectionDto(result);
  }

  @Override
  public long getYearlyInspectDownloadCount(List<String> inspectionIdList, CarYearlyInspectStatusEnum carYearlyInspectStatusEnum, List<Integer> carIds, Integer stockId, String inspectStartDate, String inspectEndDate) {
    if(null != stockId && stockId == -1) {
      return 0;
    }
    return yearlyInspectMapper.getYearlyInspectDownloadCount(inspectionIdList,carYearlyInspectStatusEnum.getValue(), carIds, inspectStartDate, inspectEndDate);
  }

  @Override
  public List<Integer> getCarIdListByHandleStatus(YearlyInspectHandleStatusEnum yearlyInspectHandleStatusEnum) {
    return yearlyInspectMapper.getCarIdListByHandleStatus(yearlyInspectHandleStatusEnum.getValue());
  }

  @Override
  public Boolean modifyYearlyInspectionBatch(List<YearlyInspectionModifyDto> list) {
      List<YearlyInspectionBean> yearlyInspectionList = Convert.toYearlyInspectionBeanList(list);
      int result = yearlyInspectMapper.modifyBatch(yearlyInspectionList);
      if(result > 0){
        return true;
      }else{
          return false;
      }
  }

    @Override
    public Boolean modifyYearlyInspection(List<Integer> carIdList) {
      if(null==carIdList||carIdList.size()<1){
          return true;
      }
      yearlyInspectMapper.modifyYearlyInspection(carIdList);
      return true;
    }

    @Override
    public List<YearlyInspectionDto> getYearlyInspectionListByCarId(List<Integer> carIdList) {
      if(null == carIdList || carIdList.size() < 1){
          return null;
      }
      List<YearlyInspectionBean> yearlyInspectionList = yearlyInspectMapper.getListByCarId(carIdList);
      return Convert.toYearlyInspectionDto(yearlyInspectionList);
    }

  @Override
  public Boolean modifyYearlyInspectionHandleStatus() {
    yearlyInspectMapper.modifyYearlyInspectionHandleStatus();
    return true;
  }


}
